#define _CRT_SECURE_NO_WARNINGS 1
#include <string>
#include <iostream>
using namespace std;

void Reverse(string& str, int f, int l)
{
     while (f < l)
    {
        char tmp = str[f];
        str[f] = str[l];
        str[l] = tmp;

        f++;
        l--;
    }
}
string test01()
{
	//char arr[10] = { 0, 1, 3 };
	//cout << sizeof(arr) / sizeof(arr[0]) << endl;
    string s("hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl");
    int k = 39;

    int first = 0;
    int last = 0;
    while (last != s.size())
    {
        if ((last - first) == (2 * k - 1))
        {
            Reverse(s, first, first + k - 1);
            first += 2 * k;
            last = first;
        }
        else
            last++;
    }

    int ret = last - first;
    if (ret > k)
    {
        Reverse(s, first, first + k - 1);
    }
    else
    {
        Reverse(s, first, last - 1);
    }
    return s;
}

void test02()
{
    void Reverse(string & s, int first, int last)
    {
        while (first < last)
        {
            char tmp = s[first];
            s[first] = s[last];
            s[last] = tmp;

            first++;
            last--;
        }
    }

    class Solution {
    public:
        string reverseStr(string s, int k) {
            int first = 0;
            int last = 0;
            while (last != s.size())
            {
                if ((last - first) == (2 * k - 1))
                {
                    Reverse(s, first, first + k - 1);
                    first += 2 * k;
                    last = first;
                }
                else
                    last++;
            }

            int ret = last - first;
            if (ret > k)
            {
                Reverse(s, first, first + k - 1);
            }
            else
            {
                Reverse(s, first, last - 1);
            }
            return s;
        }
    };
}

void test03()
{
    class Solution {
    public:

        void Reverse(string& s, int first, int last)
        {
            last--;
            while (first < last)
            {
                char tmp = s[first];
                s[first] = s[last];
                s[last] = tmp;

                first++;
                last--;
            }
        }

        string reverseStr(string s, int k) {
            int len = s.size();
            for (int i = 0; i < len; i += 2 * k)
            {
                if (i + k < len)
                {
                    Reverse(s, i, i + k);
                }
                else
                {
                    Reverse(s, i, len);
                }
            }
            return s;
        }
    };-
}

int main()
{
	test01();
    //test02();
    test03();
	return 0;
}
